package com.coach.control;



import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.coach.entity.Student;
import com.coach.entity.User;
import com.coach.service.UserService;
import com.coach.util.BaseResponse;
import com.coach.util.EhCacheUtils;
import com.coach.util.oss.Identities;



@Controller
@RequestMapping("/users")
public class UserController {
	
	
	@Autowired
	private UserService userService;
	@Autowired
	private EhCacheUtils ehcacheUtils;
	
	@RequestMapping(value="/getuser",produces="application/json",method=RequestMethod.GET)  //单个user
	@ResponseBody
	public BaseResponse<User> getUser(@PathVariable String id,HttpServletRequest request,HttpServletResponse response) throws Exception{
		 int uid=Integer.parseInt(id);
	   if(uid>0){	
		 Map<String,Object> map=new HashedMap();
		 map.put("id",uid);
		return userService.getUserList(map);
	   }else{
		   return null;
	   }
	}
	
	@RequestMapping(value="/checklogin",produces="application/json",method=RequestMethod.POST) //检查登录
	@ResponseBody
	public BaseResponse<User> checklogin(@RequestParam("username") String username,@RequestParam("password") String password) throws Exception{
		Map<String,Object> map=new HashedMap();
		map.put("username",username);
		map.put("password", password);
	   BaseResponse<User> response=userService.checkLogin(map);	
	   if(response.getData().size()>0){
		   String token = Identities.uuid4();
		   ehcacheUtils.put("LOGIN_TOKEN_"+username, token);
	   }	   
	   return response;
	}
	
	@RequestMapping(value="/checkphoneNumber",produces="application/json",method=RequestMethod.POST) //检查重复注册
	@ResponseBody
	public BaseResponse<Integer> checkphone(@RequestParam("phoneNumber") String phoneNumber) throws Exception{
	   return	userService.checkPhoneNumberExist(phoneNumber);
	}
	
	@RequestMapping(value="/list",produces="application/json",method=RequestMethod.GET) //userlist
	@ResponseBody
	public BaseResponse<User> getUserList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> map=new HashedMap();
		return userService.getUserList(map);	
	}
	
	
	
	@RequestMapping(value="/add",produces="application/json",method=RequestMethod.POST) //新增
	@ResponseBody
	public BaseResponse<User> addUser(@RequestParam("name") String name,@RequestParam("nickName") String nickName,@RequestParam("password") String password,@RequestParam("wechatId") String wechatId,
			@RequestParam("logoUrl") String logoUrl,@RequestParam("phoneNumber") String phoneNumber,@RequestParam("area") String area,@RequestParam("sex") char sex,@RequestParam("roleType") int roleType,@RequestParam("age") int age,@RequestParam("IDCard") String IDCard) throws Exception{
	    User user=new User();
	    user.setAge(age);user.setArea(area);user.setIDCard(IDCard);
	    user.setLogoUrl(logoUrl);user.setName(name);user.setNickName(nickName);user.setPassword(password);user.setPhoneNumber(phoneNumber);
	    user.setRoleType(roleType);
	    user.setSex(sex);
	    user.setSignDate(new java.sql.Date(new java.util.Date().getTime()));
		return userService.addUser(user);

	}
	
	@RequestMapping(value="/mixadd",produces="application/json",method=RequestMethod.POST) //新增,包括两张表
	@ResponseBody
	public BaseResponse<Student> addmixUser(@RequestParam("name") String name,@RequestParam("nickName") String nickName,@RequestParam("password") String password,@RequestParam("wechatId") String wechatId,
			@RequestParam("logoUrl") String logoUrl,@RequestParam("phoneNumber") String phoneNumber,@RequestParam("area") String area,@RequestParam("sex") char sex,@RequestParam("roleType") int roleType,@RequestParam("age") int age,@RequestParam("IDCard") String IDCard,
			@RequestParam("parentType") int parentType,@RequestParam("parentName") String parentName) throws Exception{
	    User user=new User();
	    user.setAge(age);user.setArea(area);user.setIDCard(IDCard);
	    user.setLogoUrl(logoUrl);user.setName(name);user.setNickName(nickName);user.setPassword(password);user.setPhoneNumber(phoneNumber);
	    user.setRoleType(roleType);
	    user.setSex(sex);
	    user.setSignDate(new java.sql.Date(new java.util.Date().getTime()));
		userService.addUser(user);
		Student st=new Student();
		st.setUserId(user.getId());
		st.setParentName(parentName);
		st.setParentType(parentType);
		return userService.addStudent(st);        
	}
	
	
	@RequestMapping(value="/{id}",produces="application/json",method=RequestMethod.PUT) //更新
	@ResponseBody
	public BaseResponse<User> updateUser(@PathVariable String id,HttpServletRequest request,HttpServletResponse response) throws Exception{
		 int uid=Integer.parseInt(id);
		   if(uid>0) {
			  User user=userService.getUserById(uid);
			  String username=request.getParameter("username");
			  if(user!=null&&username!=null&&username!=""){
				  
				  return userService.updateUser(user);
			  }
			  else {
				  return null;
			  }
		   }else{
			   return null;
		   }	  
	}
	
	@RequestMapping(value="/{id}",produces="application/json",method=RequestMethod.DELETE) //删除
	@ResponseBody
	public BaseResponse<User> deleteUser(@PathVariable String id,HttpServletRequest request,HttpServletResponse response) throws Exception{
		 System.out.println("ddddd");
	   int uid=Integer.parseInt(id);
	   if(uid>0) {
		   User user=userService.getUserById(uid);
		   if(user!=null){	
				return userService.deleteUser(user);
			   }else{
				   return null;
			   }
	   }
	   else{
		   return null;
	   }
	}
	
	
	
	
	



}
